/*
Automatic clinic tests using a robot arm.
Copyright (C) 2010  CIC-CPC, CHU Gabriel Montpied
All other trademarks, logos and copyrights are the property
of their respective owners.

This file is part of cpc-painbot.

cpc-painbot is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

cpc-painbot is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with cpc-painbot.  If not, see <http://www.gnu.org/licenses/>.
*/
#include "testindicateurprocess.h"

#include <QTest>

#include "indicateurprocess.h"
#include "commande/indproc/ascii.h"
#include "commande/indproc/reponseindicateur.h"

void TestIndicateurProcess::testCommande_data()
{
    using namespace commande::indproc;
    QList<QString> attendus;
    QTest::addColumn<Ascii*>("cmd");
    QTest::addColumn<QString>("attendu");
    QTest::addColumn<bool>("valide");

    attendus.append("*00V\r"); // Lire/Reset min
    attendus.append("*00v\r");
    attendus.append("*00P\r"); // Lire/Reset max
    attendus.append("*00p\r");
    attendus.append("*00T\r"); // Lire/Reset tare
    attendus.append("*00r\r");
    attendus.append("*00t\r"); // Prendre tare
    attendus.append("*00D\r"); // Prendre mesure

    for (int i = 0; i < Ascii::nbTypes; i++)
        QTest::newRow(QString::number(i).toAscii()) << Ascii::creer(Ascii::Type(i)) << attendus.at(i) << true;

    QTest::newRow("inexistante") << Ascii::creer(Ascii::Type(82)) << "" << false;
}

void TestIndicateurProcess::testCommande()
{
    QFETCH(commande::indproc::Ascii*, cmd);
    QFETCH(QString, attendu);
    QFETCH(bool, valide);


    if ( valide ) {
        QVERIFY(cmd != 0);
        QVERIFY(commande::indproc::Ascii::format.exactMatch(cmd->generer()) == true);
        QCOMPARE(cmd->generer(), attendu);
        QCOMPARE(cmd->generer().toAscii(), attendu.toAscii());
        delete cmd;
    } else
        QVERIFY(cmd == 0);
}

void TestIndicateurProcess::testReponseIndicateur_data()
{
    using namespace commande::indproc;
    QTest::addColumn<ReponseIndicateur*>("reponse");
    QTest::addColumn<QString>("attendu");
    QTest::addColumn<qreal>("valeur");
    QTest::addColumn<bool>("valide");

    QTest::newRow("normale")
            << ReponseIndicateur::lire(" +010.85\r")
            << " +010.85\r" << qreal(10.85) << true;

    QTest::newRow("invalide")
            << ReponseIndicateur::lire(" 42\r")
            << "" << 0. << false;

}

void TestIndicateurProcess::testReponseIndicateur()
{
    QFETCH(commande::indproc::ReponseIndicateur*, reponse);
    QFETCH(QString, attendu);
    QFETCH(qreal, valeur);
    QFETCH(bool, valide);

    if ( valide ) {
        QVERIFY(reponse != 0);
        QVERIFY(commande::indproc::ReponseIndicateur::format.exactMatch(reponse->generer()) == true);
        QCOMPARE(reponse->generer(), attendu);
        QCOMPARE(reponse->generer().toAscii(), attendu.toAscii());
        QCOMPARE(reponse->valeur(), valeur);
        delete reponse;
    } else
        QVERIFY(reponse == 0);
}
